﻿namespace MSBuild.MKS.Tasks
{
    using System;

    using Microsoft.Build.Framework;
    using Microsoft.Build.Utilities;

    /// <summary>
    /// The add task.
    /// </summary>
    public class Connect : Task
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the Host Name.
        /// </summary>
        [Required]
        public string Hostname { get; set; }

        /// <summary>
        /// Gets or sets the HostPort.
        /// </summary>
        [Required]
        public string HostPort { get; set; }

        /// <summary>
        /// Gets or sets Username.
        /// </summary>
        [Required]
        public string UserName { get; set; }

        /// <summary>
        /// Gets or sets Password.
        /// </summary>
        [Required]
        public string Password { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// The execute.
        /// </summary>
        /// <returns>
        /// The execute.
        /// </returns>
        public override bool Execute()
        {
            try
            {
                var cmds = new MksCommands
                    {
                        HostName = this.Hostname,
                        HostPort = this.HostPort,
                        User = this.UserName,
                        Password = this.Password,
                        Log = this.Log
                    };

                cmds.Connect();

                return true;
            }
            catch (Exception ex)
            {
                this.Log.LogError("Exception during {0} procedure: {1}", this.GetType().Name, ex.GetType().Name);
                this.Log.LogError("Exception details: {0}", ex.Message);
                return false;
            }
        }

        #endregion
    }
}